package com.android.tnovoselec.schedule.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.android.tnovoselec.schedule.R;
import com.android.tnovoselec.schedule.adapter.ScheduleFragmentAdapter;
import com.android.tnovoselec.schedule.async.GetAllItemsAsync;
import com.android.tnovoselec.schedule.async.GetAllItemsAsync.ItemsFetchedListener;
import com.android.tnovoselec.schedule.db.ScheduleDatabaseHelper;
import com.android.tnovoselec.schedule.dto.ItemDto;
import com.j256.ormlite.dao.Dao;
import com.tnovoselec.android.orm.apptools.OrmLiteBaseFragmentActivity;

public class MainActivity extends OrmLiteBaseFragmentActivity<ScheduleDatabaseHelper> implements ItemsFetchedListener {

	public interface ItemAddedListener {
		void onItemAdded();
	}

	public static String TAG = MainActivity.class.getSimpleName();

	private List<ItemAddedListener> listeners = new ArrayList<MainActivity.ItemAddedListener>();
	private ViewPager viewpager;
	private ScheduleFragmentAdapter adapter;
	private Dao<ItemDto, String> scheduleDao = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		viewpager = (ViewPager) findViewById(R.id.main_viewpager);

		try {
			scheduleDao = super.getHelper().getScheduleDao();
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		GetAllItemsAsync task = new GetAllItemsAsync(scheduleDao);
		task.setListener(this);
		task.execute(new Void[0]);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (item.getItemId() == R.id.main_add_new_item) {
			ItemDto newItem = new ItemDto();
			Calendar cal = Calendar.getInstance();
			int hours = cal.get(Calendar.HOUR_OF_DAY);
			int minutes = cal.get(Calendar.MINUTE);
			newItem.setDay(viewpager.getCurrentItem());
			newItem.setHoursFrom("" + hours + ":" + minutes);
			newItem.setHoursTo("" + hours + ":" + minutes);
			newItem.setOrder(0);
			newItem.setNotes("kita");
			newItem.setTitle("" + System.currentTimeMillis() / 100);
			try {
				scheduleDao.create(newItem);
			} catch (Exception e) {
				Log.e(TAG, e.getMessage());
			}
			GetAllItemsAsync task = new GetAllItemsAsync(scheduleDao);
			task.setListener(this);
			task.execute(new Void[0]);
		}

		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onItemsFetched() {
		if (adapter == null) {
			adapter = new ScheduleFragmentAdapter(getSupportFragmentManager(), this);
			viewpager.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		for (ItemAddedListener listener : listeners) {
			listener.onItemAdded();
		}

	}

	public void registerListener(ItemAddedListener listener) {
		listeners.add(listener);
	}
	public void unregisterListener(ItemAddedListener listener){
		listeners.remove(listener);
	}

}
